package com.guigu.dh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dh.cmn.feign.DictFeignClient;
import com.guigu.dh.mapper.PatientMapper;
import com.guigu.dh.pojo.user.Patient;
import com.guigu.dh.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    // 根据用户ID，查询就诊人信息列表
    @Override
    public List<Patient> getPatientList(Long userId) {
        // 封装条件
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        // 查询列表
        List<Patient> patients = this.baseMapper.selectList(wrapper);
        // 转换参数为显示值
        patients.forEach(this::setParam);
        return patients;
    }

    // 根据id查询就诊人信息
    @Override
    public Patient getPatientById(Long id) {
        return this.setParam(this.baseMapper.selectById(id));
    }

    // 封装参数
    private Patient setParam (Patient patient) {

       String certificatesTypeString = (String)
               dictFeignClient.findDictByValue("null", patient.getCertificatesType()).getData();

       // 因为联系人信息是选填的，所以需要判断是否为空
       if (patient.getContactsCertificatesType() != null && !"".equals(patient.getContactsCertificatesType())) {
           String contactsCertificatesTypeString = (String)
                   dictFeignClient.findDictByValue("null", patient.getContactsCertificatesType()).getData();
           patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
       }

        patient.getParam().put("certificatesTypeString", certificatesTypeString);

        return patient;
    }
}
